package com.totem.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.totem.base.constants.ErrorCode;
import com.totem.base.enums.CurrencyEnum;
import com.totem.base.enums.FundAmtTypeEnum;
import com.totem.base.enums.SwitchEnum;
import com.totem.base.exception.BaseException;
import com.totem.base.mybatis.UserIdContext;
import com.totem.customer.mapper.TCustomerMapper;
import com.totem.customer.model.TCustomers;
import com.totem.product.mapper.TFundsMapper;
import com.totem.product.model.TFunds;
import com.totem.product.service.ITFundService;
import com.totem.product.vo.SwitchAutoInvestVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Slf4j
@Service
public class TFundServiceImpl extends ServiceImpl<TFundsMapper, TFunds> implements ITFundService {
    @Autowired
    private TCustomerMapper tCustomerMapper;

    @Override
    public boolean initData() {
        TFunds fundOther = TFunds.builder()
                .fundName("游戏建设基金")
                .fundAmtType(FundAmtTypeEnum.FUND_OTHER.getCode())
                .currency(CurrencyEnum.USDO.name())
                .build();
        TFunds fund100= TFunds.builder()
                .fundName("游戏建设基金")
                .fundAmtType(FundAmtTypeEnum.FUND_100.getCode())
                .currency(CurrencyEnum.USDO.name())
                .build();
        TFunds fund200 = TFunds.builder()
                .fundName("游戏建设基金")
                .fundAmtType(FundAmtTypeEnum.FUND_200.getCode())
                .currency(CurrencyEnum.USDO.name())
                .build();
        TFunds fund300 = TFunds.builder()
                .fundName("游戏建设基金")
                .fundAmtType(FundAmtTypeEnum.FUND_300.getCode())
                .currency(CurrencyEnum.USDO.name())
                .build();
        List<TFunds> initData = List.of(fundOther, fund100, fund200, fund300);

        LambdaQueryWrapper<TFunds> fundQuery = new LambdaQueryWrapper<>();
        Long count = baseMapper.selectCount(fundQuery);
        if(count>=initData.size()){
            return true;
        }

        return super.saveBatch(initData);
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public boolean switchAutoInvest(SwitchAutoInvestVO switchAutoInvestVO) {
        Long customerId = UserIdContext.getUserId();
        if(customerId==null){
            throw new BaseException(ErrorCode.UNAUTHORIZED);
        }

        SwitchEnum switchEnum = SwitchEnum.byCode(switchAutoInvestVO.getSwitchAutoInvest());
        if(switchEnum==null){
            throw new BaseException(ErrorCode.ILLEGAL_SWITCH);
        }

        TCustomers update = TCustomers.builder()
                .id(customerId)
                .fundAutoInvest(switchEnum.getCode())
                .build();

        int effect = tCustomerMapper.updateById(update);
        return effect>0;
    }
}
